package com.jt.tenant.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.SetMaintenanceTime;
import com.jt.tenant.dialog.MyTimePopup;
import com.jt.tenant.fragment.CarFragment;
import com.jt.tenant.popwindow.SelectPopup;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 保养提醒
 */
public class MaintaenanceSettingActivity extends JTActivity implements View.OnClickListener {

    private TextView title_name;

    private TextView bottom_text;

    private TextView tv_lasttime_maintain_time;

    private EditText et_lasttime_maintain_km;

    private TextView tv_lasttime_maintain_time_space;

    private TextView et_lasttime_maintain_km_space;

    private MyTimePopup myTimePopup;

    private Calendar calendar = Calendar.getInstance();

    private SetMaintenanceTime setMaintenanceTime;

    private String mTime = "";

    private String month_space = "",km_space = "";

    private String remind = "1";

    private List<String> month_list = new ArrayList<>();

    private List<String> km_list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_maintain_setting);

        initView();

        myTimePopup = new MyTimePopup(context, null, new Date(), MyTimePopup.Type.YEAR_MONTH_DAY) {

            private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            @Override
            public void onSubmit(Date date) {

                tv_lasttime_maintain_time.setText(new SimpleDateFormat("yyyy-MM-dd").format(date));
                mTime = new SimpleDateFormat("yyyy-MM-dd").format(date);

            }

        };

        myTimePopup.setTime(calendar.getTime());

    }

    private void initView() {

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("保养设定");

        (bottom_text = (TextView) findViewById(R.id.btn_maintain_setting_commit)).setOnClickListener(this);

        et_lasttime_maintain_km = (EditText) findViewById(R.id.et_lasttime_maintain_km);

        (tv_lasttime_maintain_time = (TextView) findViewById(R.id.tv_lasttime_maintain_time)).setOnClickListener(this);

        (et_lasttime_maintain_km_space = (TextView) findViewById(R.id.et_lasttime_maintain_km_space)).setOnClickListener(this);

        (tv_lasttime_maintain_time_space = (TextView) findViewById(R.id.tv_lasttime_maintain_time_space)).setOnClickListener(this);


        for(int i = 1;i<13;i++){

            month_list.add(String.valueOf(i)+"个月");

        }

        for(int j = 1;j<11;j++){

            km_list.add(String.valueOf(j*1000)+"公里");

        }

    }

    @Override
    public void onClick(final View v) {

        switch (v.getId()) {


            //上一次保养时间
            case R.id.tv_lasttime_maintain_time:

                //先关闭软键盘
                View view = getWindow().peekDecorView();

                if (view != null) {

                    InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                    inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);

                }

                myTimePopup.show(getWindow().getDecorView());

                break;

            case R.id.tv_lasttime_maintain_time_space :

                SelectPopup selectPopup = new SelectPopup(this, month_list);

                selectPopup.setOnTimeSelectListener(new SelectPopup.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(String value) {

                        tv_lasttime_maintain_time_space.setText(value);

                        month_space = value.replace("个月","");

                    }

                });

                selectPopup.showAtLocation(v,Gravity.BOTTOM, 0, 0);

                break;

            case R.id.et_lasttime_maintain_km_space :

                SelectPopup selectPopup_km = new SelectPopup(this, km_list);

                selectPopup_km.setOnTimeSelectListener(new SelectPopup.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(String value) {

                        et_lasttime_maintain_km_space.setText(value);

                        km_space = value.replace("公里","");

                    }

                });

                selectPopup_km.showAtLocation(v,Gravity.BOTTOM, 0, 0);

                break;
            case R.id.btn_maintain_setting_commit:

                if (mTime.equals("")) {
                    UtilToast.show(context, "上次保养时间不能为空");
                } else if (et_lasttime_maintain_km.getText().toString().equals("")) {
                    UtilToast.show(context, "上次保养公里数不能为空");
                } else if (month_space.equals("")) {
                    UtilToast.show(context, "保养时间间隔不能为空");
                }else if (km_space.equals("")) {
                    UtilToast.show(context, "保养公里数间隔不能为空");
                }else {

                    setMaintenanceTime = new SetMaintenanceTime(new AsyCallBack<SetMaintenanceTime.Info>() {
                        @Override
                        public void onSuccess(String toast, int type, SetMaintenanceTime.Info info) throws Exception {
                            super.onSuccess(toast, type, info);
                            if (info.msg.equals("000")) {

                                UtilToast.show(context, "提交成功");

                                if(remind.equals("0")){

                                    JTApplication.jtPreferences.saveMessage(false,"6");

                                }else {

                                    JTApplication.jtPreferences.saveMessage(true,"6");

                                }

                                if(CarFragment.refreshLinster != null){

                                    CarFragment.refreshLinster.refresh();

                                }

                                finish();

                            } else {
                                UtilToast.show(context, info.msg);
                            }
                        }

                        @Override
                        public void onFail(String toast, int type) throws Exception {
                            super.onFail(toast, type);
                        }
                    });
                    setMaintenanceTime.lastMaintenanceTime = mTime;
                    setMaintenanceTime.lastMaintenanceDistance = et_lasttime_maintain_km.getText().toString();
                    setMaintenanceTime.MaintenanceDistanceSpace = km_space;
                    setMaintenanceTime.MaintenanceTimeSpace = month_space;
                    setMaintenanceTime.remind = remind;
                    setMaintenanceTime.execute(getApplicationContext());
                }

                break;



        }

    }

}

